﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.DataOperate;

namespace BLL.Business
{
    public class ShoppingCarBLL
    {
        DAL.DataOperate.ShoppingCarDAL carDAL = new ShoppingCarDAL();
        public DataSet SelectByUserId(int UserId)
        {
            if (UserId.ToString() == "" || UserId.ToString() == null)
            { 
                return null; 
            }
            else
            { 
                return carDAL.SelectByUserId(UserId); 
            }
        }
        public DataSet SelectByBookUserId(int BookId, int UserId)
        { 
            if((BookId.ToString() == null || BookId.ToString() == "" ) ||
               (UserId.ToString() == null || UserId.ToString() == ""))
            {
                return null;
            }
            else
            {
                return carDAL.SelectByBookUserId(BookId,UserId);
            }
        }
        public bool Insert(Model.ShoppingCar car)
        {
            if ((car.Book_Idint.ToString() == "" || car.Book_Idint.ToString() == null) ||
                (car.User_Idint.ToString() == "" || car.User_Idint.ToString() == null))
            {
                return false;
            }
            else
            {
                DataSet ds = new DataSet();
                ds = SelectByBookUserId(car.Book_Idint, car.User_Idint);

                if ((ds == null) ||                           // 如果不存在就可以添加
                (ds.Tables.Count == 0) ||
                (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    return carDAL.Insert(car);
                }
                else
                {
                    return true;
                }
            }
        }
        public bool Delete(int CarId)
        {
            if (CarId.ToString() == "" || CarId.ToString() == null)
            {
                return false;
            }
            else
            {
                return carDAL.Delete(CarId);
            }
        }
        public bool Delete(int BookId, String UserName)
        {
            if ((BookId.ToString() == "" || BookId.ToString() == null) ||
                (UserName.ToString() == "" || UserName.ToString() == null))
            {
                return false;
            }
            else
            {
                return carDAL.Delete(BookId, UserName);
            }
        }
    }
}
